package com.getinstaliker.instagram;

import java.util.List;

import org.apache.http.NameValuePair;

import android.content.Context;

import com.getinstaliker.util.network.httptask.HttpPostStringTask;
import com.getinstaliker.util.network.httptask.MobiNetworkJsonTask;

public class InstagramBaseTask {

	/**
	 * Execute http get task.
	 *
	 * @param <T> the generic type
	 * @param context the context
	 * @param url the url
	 * @param listener the listener
	 * @param returnType the return type
	 */
	public static <T> void executeHttpGetTask(Context context,String url,final InstagramTaskListener<T> listener,final Class<T> returnType){
		MobiNetworkJsonTask<String, T> getResultHttpGetTask = new MobiNetworkJsonTask<String, T>(context,true) {

			@Override
			public void onUIUpDate(T result) {
				if(listener!=null){
					listener.onTaskComplete(result);
				}
			}

			@Override
			protected Class<T> getType() {
				return returnType;
			}
		};
		getResultHttpGetTask.execute(url);
	}
	
	/**
	 * Execute http post task.
	 *
	 * @param <T> the generic type
	 * @param context the context
	 * @param url the url
	 * @param pairs the pairs
	 * @param listener the listener
	 * @param returnType the return type
	 */
	public static <T> void executeHttpPostTask(Context context,String url,List<NameValuePair> pairs,final InstagramTaskListener<T> listener,final Class<T> returnType){
		HttpPostStringTask<String, T> getResultHttpPostTask = 
				new HttpPostStringTask<String, T>(context,true,pairs) {
			@Override
			public void onUIUpDate(T result) {
				if(listener!=null){
					listener.onTaskComplete(result);
				}
			}

			@Override
			protected Class<T> getType() {
				return returnType;
			}
		};
		getResultHttpPostTask.execute(url);
	}
	/**
	 * The listener interface for receiving instagramTask events.
	 * The class that is interested in processing a instagramTask
	 * event implements this interface, and the object created
	 * with that class is registered with a component using the
	 * component's <code>addInstagramTaskListener<code> method. When
	 * the instagramTask event occurs, that object's appropriate
	 * method is invoked.
	 *
	 * @param <T> the generic type
	 * @see InstagramTaskEvent
	 */
	public interface InstagramTaskListener<T>{
		
		/**
		 * On task complete.
		 *
		 * @param ob the ob
		 */
		public void onTaskComplete(T ob);
	}
}
